package com.faithbj.danaliverpool.web.controller.admin;

import com.faithbj.common.configuration.Pager;
import com.faithbj.common.model.Region;
import com.faithbj.common.service.RegionService;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author jrn E-mail: rinanjin@gmail.com
 * @version 0.1 
 * 创建时间：2012-6-8 下午11:08:08
 * 类说明
 */
@Controller
@RequestMapping("/faith/region")
public class AdminRegionController extends BaseAdminController
{
	private static final long serialVersionUID = 1L;

	@Resource
	private RegionService regionService;
	
	private final String LIST = "/admin/region_list";
	
	private final String INPUT = "/admin/region_input";
	
	private final String OPERRATE_RETURN_URL = "/faith/region/list";
	
	@Value("${baseurl}")
	private String base;

	public String getBase()
	{
		return base;
	}

	public void setBase(String base)
	{
		this.base = base;
	}
	
	@RequestMapping(value = "/list")
	public String list(Pager pager, ModelMap map)
	{
		Pager pagerVal = regionService.findByPager(pager);
		map.put("pager", pagerVal);
		return LIST;
	}
	
	@SuppressWarnings("rawtypes")
	@ResponseBody
	@RequestMapping(value = "getListByType")
	public Map getListByType(@RequestParam("type") Integer type, ModelMap map)
	{
		List<Region> regionList = regionService.getList("regionType", type - 1);
		for(Region region : regionList)
		{
			region.setParent(null);
			region.setChildren(null);
		}
		map.put("regionList", regionList);
		return map;
	}
	
	@RequestMapping(value = "add")
	public String add(ModelMap map)
	{
		map.put("isAddAction", true);
		return INPUT;
	}
	
	@RequestMapping(value = "save")
	public String save(@Valid Region region, Errors errors, ModelMap map)
	{
		if(regionService.get("name", region.getName()) != null)
		{
			map.put("error1", "已存在同名机构");
			map.put("region", region);
			map.put("isAddAction", true);
			return INPUT;
		}
		
		if(regionService.get("code", region.getCode()) != null)
		{
			map.put("error2", "已存在相同编码,请重新填写");
			map.put("region", region);
			map.put("isAddAction", true);
			return INPUT;
		}
		
		if(errors.hasErrors())
		{
			map.put("isAddAction", true);
			return INPUT;
		}
		
		String pid = region.getParent().getId();
		if(!StringUtils.isEmpty(pid))
		{
			Region parent = regionService.get(pid);
			region.setParent(parent);
		}
		regionService.save(region);
		regionService.flush();

		map.put("redirectUrl", OPERRATE_RETURN_URL);
		return SUCCESS;
	}
	
	@RequestMapping(value = "delete")
	public String delete(@RequestParam("id") String id, ModelMap map)
	{
		regionService.delete(id);
		regionService.flush();
		
		map.put("redirectUrl", OPERRATE_RETURN_URL);
		return SUCCESS;
	}
}
